﻿using DMC300Axis_IO.Function;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using csLTDMC;
using OCR;
using System.Windows.Forms;
using OCR.Config;
using OCR.SystemThread;
using System.Drawing;
using System.IO;

namespace OCR.SystemThread
{
    class CameraSingleTriggerThread
    {
        public static bool CameraSingleTrigger_Flowrec = false;
        public static bool CameraSingleTrigger_OnLive = false;
        public static Thread CameraSingleTriggerFlow_Thread = null;
        public static int iCameraSingleTrigger_Step = 0;

        public static bool CameraTrigger_Stop = false;

        public static Bitmap finalBit;

        public static void CameraSingleTrigger_Thread()
        {
            CameraSingleTrigger_Flowrec = true;
            CameraSingleTriggerFlow_Thread = new Thread(CameraSingleTriggerFlowThread);
            CameraSingleTriggerFlow_Thread.IsBackground = true;
            CameraSingleTriggerFlow_Thread.Start();
            Console.WriteLine("iCameraSingleTrigger_Step: " + iCameraSingleTrigger_Step);
        }
        //-------------------------------------------------------------------------------
        //---自动运行流程----------------------------------------------------------------
        private static void CameraSingleTriggerFlowThread(object obj)
        {
            while (CameraSingleTrigger_Flowrec)
            {
                switch (iCameraSingleTrigger_Step)
                {
                    case 0:

                        break;
                    case 1:
                        break;
                    case 10://  启动触发相机流程，并等待产品触碰到感测器
                        {
                            break;
                        }
                    case 15:
                        {

                            break;
                        }
                    case 20:   //输出讯号触发相机取像
                        {
                            IOThread_Run.DO_Trigger_olng(0, 1);
                            iCameraSingleTrigger_Step = 21;
                            break;
                        }
                    case 21:
                        {
                            if (CameraReceiveThread.isImageData)
                            {

                                IOThread_Run.DO_Trigger_olng(0, 0);
                                iCameraSingleTrigger_Step = 22;
                            }

                            break;
                        }
                    case 22:

                        if (CameraReceiveThread.isImageData)
                        {

                            //裁剪拼接
                            List<Rectangle> roiLocations = new List<Rectangle>();
                            Bitmap cropBit = OCR_Form.StitchROIs_Middle(CameraReceiveThread.frameForSave.Image.ToBitmap(), LocalTemplateSingleInfos_Config.rectangles, out roiLocations);
                            //AlgorithmModule_GPU.ImageHandleV2_ByteArray(cropBit);
                            AlgorithmModule_GPU.ImageHandleV2_Opencv(cropBit);
                            //映射回原图
                            List<Rectangle> convertRectangles = OCR_Form.MapBackToOriginal(OCR_Form.ConvertToRectangles(AlgorithmModule_GPU.pointsList), roiLocations, LocalTemplateSingleInfos_Config.rectangles);
                            AlgorithmModule_GPU.pointsList = OCR_Form.ConvertToPointLists(convertRectangles);

                            //AlgorithmModule_GPU.ImageHandleV2_ByteArray(CameraReceiveThread.frameForSave.Image.ToBitmap());

                            //AlgorithmModule_GPU.ImageHandleV2_ByteArray(CameraReceiveThread.frameForSave.Image.PixelData, CameraReceiveThread.frameForSave.Image.Width, CameraReceiveThread.frameForSave.Image.Height, false);

                            LocalInfo.totalCount += LocalInfo.Tem_RectMaxCount;
                            Image image = CameraReceiveThread.frameForSave.Image.ToBitmap();
                            finalBit = new Bitmap(image);


                            LocalTemplateSingleInfos_Config.finalPointsList.Clear();
                            LocalTemplateSingleInfos_Config.mainInfos.Clear();
                            LocalTemplateSingleInfos_Config.temInfos.Clear();
                            for (int i = 0; i < LocalTemplateSingleInfos_Config.localTemplateSingleInfos.Count; i++)
                            {
                                for (int j = 0; j < LocalTemplateSingleInfos_Config.localTemplateSingleInfos[i].InfoList.Count; j++)
                                {
                                    LocalTemplateSingleInfos_Config.temInfos.Add(LocalTemplateSingleInfos_Config.localTemplateSingleInfos[i].InfoList[j]);
                                    Console.WriteLine("temInfos.Count:" + LocalTemplateSingleInfos_Config.temInfos.Count);
                                }

                            }

                            List<bool> isSameList = new List<bool>();
                            for (int i = 0; i < LocalTemplateSingleInfos_Config.localTemplateSingleInfos.Count; i++)
                            {
                                //LocalTemplateSingleInfos_Config.Compare(LocalTemplateSingleInfos_Config.rectangles[i], LocalTemplateSingleInfos_Config.infosList[i], LocalTemplateSingleInfos_Config.insideList[i], LocalTemplateSingleInfos_Config.finalPointsLists[i]);
                                LocalTemplateSingleInfos_Config.CompareConfuse(LocalTemplateSingleInfos_Config.rectangles[i], LocalTemplateSingleInfos_Config.infosList[i], LocalTemplateSingleInfos_Config.insideList[i], LocalTemplateSingleInfos_Config.finalPointsLists[i], LocalTemplateSingleInfos_Config.confuseMultiIndex[i], LocalTemplateSingleInfos_Config.confuseMultiIndexDic[i]);
                            }

                            for (int i = 0; i < LocalTemplateSingleInfos_Config.localTemplateSingleInfos.Count; i++)
                            {
                                isSameList.Add(LocalTemplateSingleInfos_Config.CompareTemplate_Disorder(LocalTemplateSingleInfos_Config.localTemplateSingleInfos[i].InfoList, LocalTemplateSingleInfos_Config.infosList[i]));
                            }
                            Console.WriteLine("isSameList:" + isSameList.All(x => x));
                            bool isSame = isSameList.All(x => x);

                            //LocalTemplateSingleInfos_Config.CompareTemplate_Disorder_AllInfo(LocalTemplateSingleInfos_Config.temInfos, LocalTemplateSingleInfos_Config.mainInfos);

                            if (LocalInfo.NGControlType == "1")
                            {
                                if (LocalInfo.ngOrOkSignal == "0")
                                {
                                    for (int i = 0; i < isSameList.Count; i++)
                                    {
                                        if (!isSameList[i])
                                        {
                                            IOThread_Run.DO_Trigger_olng((ushort)(i + 1), 1);
                                            Console.WriteLine("ng-------------DO-" + (i + 1).ToString());
                                        }
                                    }
                                    SendIO_NGThread_All.iSendIO_NG_Step = 5;
                                }
                                else if (LocalInfo.ngOrOkSignal == "1")
                                {
                                    for (int i = 0; i < isSameList.Count; i++)
                                    {
                                        if (isSameList[i])
                                        {
                                            IOThread_Run.DO_Trigger_olng((ushort)(i + 1), 1);
                                            Console.WriteLine("ok-------------DO-" + (i + 1).ToString());
                                        }
                                    }

                                    SendIO_NGThread_All.iSendIO_NG_Step = 5;
                                }
                            }

                            //Console.WriteLine(isSame);
                            if (!isSame)
                            {
                                Console.WriteLine("GPU-NG");


                                Console.WriteLine("isSameList:" + isSameList.ToArray().Count(b => b == false));

                                Console.WriteLine("isSameList:" + Array.FindAll(isSameList.ToArray(), b => !b).Length);

                                int tempNG_Count = isSameList.ToArray().Count(b => b == false);
                                LocalInfo.ngCount += tempNG_Count;

                                //SetFinalInfoThread.iSetFinalInfo_Step = 5;

                                FinalInfoData finalInfoData = new FinalInfoData();
                                finalInfoData.Bitmap = finalBit;
                                //finalInfoData.Bitmap = (Bitmap)finalBit.Clone();
                                finalInfoData.result = "NG";
                                finalInfoData.wrongIndex = LocalInfo.wrongIndex;
                                finalInfoData.wrongString = LocalInfo.wrongString;
                                finalInfoData.isSame = isSameList;
                                OCR_Form.infoQueue.Enqueue(finalInfoData);

                                if (LocalInfo.NGControlType == "0")
                                {
                                    NGControlThread.infoQueue.Enqueue(finalInfoData);
                                    Console.WriteLine("检测队列添加NG后:------------------------" + NGControlThread.infoQueue.Count);
                                }




                            }
                            else
                            {
                                Console.WriteLine("GPU-OK");
 

                                //SetFinalInfoThread.iSetFinalInfo_Step = 10;

                                FinalInfoData finalInfoData = new FinalInfoData();
                                finalInfoData.Bitmap = finalBit;
                                //finalInfoData.Bitmap = (Bitmap)finalBit.Clone();
                                finalInfoData.result = "OK";
                                finalInfoData.wrongIndex = LocalInfo.wrongIndex;
                                finalInfoData.wrongString = LocalInfo.wrongString;
                                finalInfoData.isSame = isSameList;
                                OCR_Form.infoQueue.Enqueue(finalInfoData);

                                if (LocalInfo.NGControlType == "0")
                                {
                                    NGControlThread.infoQueue.Enqueue(finalInfoData);
                                    Console.WriteLine("检测队列添加OK后:------------------------" + NGControlThread.infoQueue.Count);
                                }
                            }

                            CameraReceiveThread.isImageData = false;
                            CameraReceiveThread.frameForSave = null;
                            iCameraSingleTrigger_Step = 10;

                        }
                        break;
                    case 25:  //直接判断NG
                        {

                            break;
                        }
                    case 30:  //
                        {

                            break;
                        }
                    case 35:
                        {

                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
                Thread.Sleep(1);
            }
        }
    }
}
